use crate::com::Com;
use crate::myerror::MyErrs;
use std::io::stdout;
use std::io::Write;
impl Com {
    fn print_prefix() {
        print!("\x1b[0;32m==>\x1b[0m");
        stdout().flush().unwrap();
    }
    pub fn new() -> Result<Self, MyErrs> {
        let mut com = String::new();
        Self::print_prefix();
        std::io::stdin().read_line(&mut com)?;
        com = com.replace("\n", "");
        let com_and_arg: Vec<&str> = com.split_whitespace().collect();
        match com_and_arg[0] {
            "setUID" => match com_and_arg.get(1) {
                None => return Err(MyErrs::LackArg(String::from("No UID input!"))),
                Some(a) => return Ok(Com::SetUID(a.parse::<usize>()?)),
            },
            "defaults" => return Ok(Com::DefaultArticles),
            "all" => return Ok(Com::AllArcticles),
            "one_column" => match com_and_arg.get(1) {
                None => return Err(MyErrs::LackArg(String::from("No SPECIAL CLOUMN ID input!"))),
                Some(a) => return Ok(Com::InOneColumn(a.parse()?)),
            },
            "specials" => return Ok(Com::SpecialColumns),
            "clean" => return Ok(Com::Clean),
            "start" => return Ok(Com::Start),
            "" => return Ok(Com::NULL),
            "help" => {
                println!("commands: setUID [UID]\ndefaults\nall\none_column [colunm id]\nspecials\nclean\nstart");
                return Ok(Com::NULL);
            }
            "health" => {
                println!("cheak download health!");
                return Ok(Com::Health);
            }
            "exit" => {
                println!("Try to exit...");
                return Ok(Com::Exit);
            }
            _ => {
                return Err(MyErrs::UnkownCom(format!(
                    "Unkown Command: {}",
                    com_and_arg[0]
                )));
            }
        }
    }
}
